﻿
using System;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Linq;

 class Server
    {

        private TcpListener tcpListener;
        private Thread listenThread;

        public Server(int port)
        {
            Console.WriteLine("Server Started...");
            this.tcpListener = new TcpListener(new IPAddress(new byte[] { 192, 168, 0, 3 }), port);
            this.listenThread = new Thread(new ThreadStart(ListenForClients));
            this.listenThread.Start();
        }


        private void ListenForClients()
        {
            this.tcpListener.Start();

            for (; ; )
            {
                //blocks until a client has connected to the server
                TcpClient client = this.tcpListener.AcceptTcpClient();
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }

        }

        private void HandleClientComm(object client)
        {
            Client user = new Client();

           

                if (!user.loggedin)
                {
                    Login log = new Login();
                    log.login(client);
                    user.loggedin = true;
                    user.zone1 = true;
                    user.close((TcpClient)client);
                
                }
                else if (user.zone1)
                {
                    Zone zne = new Zone();
                    for (; ; )//-----------------------------------
                    {
                    zne.zone(client);
                    }//keep connection open forever or until client dc
                }


      


        }
  
    }
